package com.cheer.ba.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.MyOrderModel;
import com.cheer.ba.presenter.impl.CloudBookPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.LogUtils;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.swiperefresh.SwipyRefreshLayout;
import com.cheer.ba.widget.swiperefresh.SwipyRefreshLayoutDirection;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 描述 ：发票管理界面
 * 作者 ：hq
 * 时间 ：2017/5/24 13:21
 */
public class MyInvoicesAty extends BaseCommAty implements SwipyRefreshLayout.OnRefreshListener {

    //无数据
    @BindView(R.id.id_no_invoice_txt)
    TextView mTxtError;

    //有数据界面
    /**
     * 刷新
     */
    @BindView(R.id.id_invoice_refresh)
    SwipyRefreshLayout mRefreshLayout;
    /**
     * 滑动删除
     */
    @BindView(R.id.id_invoice_listv)
    ListView mListView;
    private CommonAdapter<MyOrderModel> mAdapter;
    private List<MyOrderModel> mInvoices = new ArrayList<MyOrderModel>();

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_ask_invoice, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return !mInvoices.isEmpty();
    }

    @Override
    public void initPresenter() {
        presenter = new CloudBookPresenter();
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.invoice_manager));
        // 设置刷新时动画的颜色，可以设置4个
        mRefreshLayout.setColorSchemeColors(Constants.COLOR_SCHEMES);
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setDirection(SwipyRefreshLayoutDirection.BOTH);
    }

    @Override
    public void initData() {
        mAdapter = new CommonAdapter<MyOrderModel>(aty, mInvoices, R.layout.adp_manager_invoice) {
            @Override
            public void convert(final ViewHolder helper, final MyOrderModel item) {
                if (null != item) {
                    helper.setText(R.id.id_invoice_time_txt, StringUtils.isEmpty(item.getCompleteTime() + "") ? StringUtils.stampToDate(item.getCreateTime() + "", "yyyy-MM-dd HH:mm:ss") : StringUtils.stampToDate(item.getCompleteTime() + "", "yyyy-MM-dd HH:mm:ss"));
                    helper.setText(R.id.id_invoice_desc_txt, item.getOrderDesc() + "  " + Currency.returnDollar(item.getCost()) + "  " + item.getProductDesc());
                    helper.setText(R.id.id_invoice_money_txt, Currency.returnDollar(item.getCostTrue()));
                    helper.setText(R.id.id_invoice_order_txt, item.getOrderId());
                    helper.getViewById(R.id.id_ask_invoice_txt).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //申请发票
                            if (null != item) {
                                Intent intent = new Intent(aty, AskInvoiceAty.class);
                                intent.putExtra(IntentKey.General.KEY_MODEL, item);
                                startActivityForResult(intent, IntentCode.Invoice.INVOICE_REQUEST_CODE);
                            }
                        }
                    });
                }
            }
        };
        mListView.setAdapter(mAdapter);
        refreshData();
    }

    /**
     * 刷新数据
     */
    public void refreshData() {
        if (hasDataInPage()) {
            mRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    mRefreshLayout.setRefreshing(true);
                }
            });
        }
        ((CloudBookPresenter) presenter).searchMyInvoices(false);
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(ConstantTag.GET_LIST_SUCCESS, tag)) {
                mRefreshLayout.setRefreshing(false);
                List<MyOrderModel> invoices = (List<MyOrderModel>) t;
                LogUtils.LogD("发票", "1111111进来了");
                if (invoices.isEmpty()) {
                    if (loadMore) {
                        ToastUtil.showToast(getString(R.string.no_more_data));
                    } else {
                        mTxtError.setVisibility(View.VISIBLE);
                        mRefreshLayout.setVisibility(View.GONE);
                    }
                } else {
                    mTxtError.setVisibility(View.GONE);
                    mRefreshLayout.setVisibility(View.VISIBLE);
                    if (!loadMore) {
                        mInvoices.clear();
                    }
                    mInvoices.addAll(invoices);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void handlerByPage(int flag1, int flag2, boolean loadMore) {
        mTxtError.setVisibility(View.VISIBLE);
        mRefreshLayout.setVisibility(View.GONE);
        if (flag1 == CodeStatus.Gegeneral.ERROR_NET) {

        } else if (flag1 == CodeStatus.Gegeneral.ERROR_DATA) {
            if (loadMore) {
                ToastUtil.showToast("数据加载失败");
            } else {
            }
        } else if (flag1 == CodeStatus.Gegeneral.DATA_SUCCESS_FULL) {
            mTxtError.setVisibility(View.GONE);
            mRefreshLayout.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onRefresh(SwipyRefreshLayoutDirection direction) {
        ((CloudBookPresenter) presenter).searchMyInvoices(direction != SwipyRefreshLayoutDirection.TOP);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IntentCode.Invoice.INVOICE_REQUEST_CODE) {
            if (resultCode == IntentCode.Invoice.INVOICE_RESULT_CODE) {
                //添加成功，刷新数据
                refreshData();
            }
        }
    }
}
